"""
Case Type   : 发布订阅
Case Name   : 以二进制模式发布内容为MOT表
Create At   : 2022-11-04
Owner       : opentestcase026
Description :
    1.在两个集群创建表
    2.创建发布端
    3.创建订阅指定以二进制方式传输
    4.发布端插入数据
    5.订阅端查询是否同步
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.mot表数据不同步,其余表数据同步
    6.成功
History     :
    Modified by opentestcase012 2023/5/6:优化用例,关闭基础数据同步
    Modified by opentestcase024 2023/8/23:增加订阅端同步等待,确保订阅端同步
"""

import os
import time
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.constant = Constant()
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.tb_name1 = 't_pub_sub_0175_1'
        self.tb_name2 = 't_pub_sub_0175_2'
        self.subname = "s_pub_sub_0175"
        self.pubname = "p_pub_sub_0175"
        self.parent_path_pub = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.parent_path_sub = os.path.dirname(macro.DB_INSTANCE_PATH_REMOTE1)
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.wal_level = self.com_pub.show_param("wal_level")
        self.enable_incremental_checkpoint = \
            self.com_pub.show_param("enable_incremental_checkpoint")
        self.enable_incremental_checkpoint1 = \
            self.com_sub.show_param("enable_incremental_checkpoint",
                                    macro.DB_ENV_PATH_REMOTE1)
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
                              f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
                              f'-W {self.pri_userdb_sub.db_password}'
        cmd = f"cp " \
              f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')} " \
              f"{os.path.join(self.parent_path_pub, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(result)
        cmd = f"cp " \
              f"{os.path.join(macro.DB_INSTANCE_PATH_REMOTE1, 'pg_hba.conf')}" \
              f" {os.path.join(self.parent_path_sub, 'pg_hba.conf')};"
        self.log.info(cmd)
        result = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(result)
        self.hostname = self.pri_userdb_sub.sh('hostname').result().strip()
        text = "--step:预置条件,修改pg_hba expect:成功"
        self.log.info(text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    replication  {self.pri_userdb_sub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host    all  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        result = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
        self.assertTrue(result, '执行失败' + text)
        result = self.commsh_pub.restart_db_cluster(True)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host    replication  {self.pri_userdb_pub.db_user} '
            f'{self.pri_userdb_pub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        guc_res = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host all  {self.pri_userdb_sub.db_user} '
            f'{self.pri_userdb_sub.db_host}/32 sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res)
        self.assertTrue(guc_res, '执行失败' + text)
        pub_result = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'enable_incremental_checkpoint=off')
        self.log.info(pub_result)
        self.assertTrue(pub_result, '执行失败' + text)
        result = self.commsh_pub.restart_db_cluster(True)
        self.log.info(result)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败' + text)
        sub_result = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"enable_incremental_checkpoint=off",
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        result = self.commsh_sub.restart_db_cluster(
            True, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result)
        flg = self.constant.START_SUCCESS_MSG in result or 'Degrade' in result
        self.assertTrue(flg, '执行失败' + text)

    def test_pub_sub(self):
        text = "--step1:在两个集群创建表 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name1};" \
                  f"drop foreign table if exists {self.tb_name2};" \
                  f"create table {self.tb_name1}(c_int int primary key,c_text " \
                  f"text) with (storage_type='astore');" \
                  f"create foreign table {self.tb_name2}(c_int int primary key," \
                  f"c_test varchar(10));"
        pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, pub_result, '执行失败'
                      + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, pub_result,
                      '执行失败' + text)
        sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sub_result, '执行失败'
                      + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, sub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, sub_result,
                      '执行失败' + text)

        text = "--step2:创建发布端 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop publication if exists {self.pubname};" \
                  f"create publication {self.pubname} for all tables;"
        pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertIn(self.constant.drop_pub_succ_msg, pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_pub_succ_msg, pub_result,
                      '执行失败' + text)

        text = "--step3:创建订阅指定以二进制方式传输 expect:成功--"
        self.log.info(text)
        result = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.create_keycipher_success, result,
                      '执行失败' + text)
        sql_cmd = f"drop subscription if exists {self.subname};" \
                  f"create subscription {self.subname} connection " \
                  f"'host={self.pri_userdb_pub.db_host} " \
                  f"port={self.port} user={self.pri_userdb_pub.db_user} " \
                  f"dbname={self.pri_userdb_pub.db_name} " \
                  f"password={self.pri_userdb_pub.ssh_password}' publication " \
                  f"{self.pubname} with (binary=True, copy_data=false);"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None,
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn(self.constant.drop_sub_succ_msg, sub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.create_sub_succ_msg, sub_result, '执行失败'
                      + text)

        text = "--step4:发布端插入数据 expect:成功--"
        self.log.info(text)
        sql_cmd = f"insert into {self.tb_name2} values(1,'first');" \
                  f"insert into {self.tb_name1} values(1,'first');"
        pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         '执行失败' + text)

        self.log.info("----等待订阅端数据同步，超时时间为20s----")
        self.commsh_pub.check_sub_consistency(self.subname,
                                              self.user_param_pub, 20)

        text = "--step5:订阅端查询是否同步 expect:mot表数据不同步,其余表数据同步--"
        self.log.info(text)
        sql_select1 = f"select * from {self.tb_name1};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select1, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertIn("1 | first", sub_result, '执行失败' + text)
        sql_select2 = f"select * from {self.tb_name2};"
        sub_result = self.commsh_sub.execut_db_sql(
            sql_select2, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_result)
        self.assertNotIn("1 | first", sub_result, '执行失败' + text)

    def tearDown(self):
        text = "--step6:清理环境 expect:成功--"
        self.log.info(text)
        sql_cmd = f"drop subscription if exists {self.subname};"
        drop_sub_result = self.commsh_sub.execut_db_sql(
            sql_cmd, self.user_param_sub, None, macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_result)
        sql_cmd = f"drop publication if exists {self.pubname};"
        drop_pub_result = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(drop_pub_result)
        sql_cmd = f"drop table if exists {self.tb_name1};" \
                  f"drop foreign table if exists {self.tb_name2};checkpoint;"
        result_sub = \
            self.commsh_sub.execut_db_sql(sql_cmd, self.user_param_sub, None,
                                          macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result_sub)
        result_pub = self.commsh_pub.execut_db_sql(
            sql_cmd, sql_type=self.user_param_pub)
        self.log.info(result_pub)
        cmd = f"mv " \
              f"{os.path.join(self.parent_path_pub, 'pg_hba.conf')} " \
              f"{os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')} "
        self.log.info(cmd)
        sh_result = self.pri_userdb_pub.sh(cmd).result()
        self.log.info(sh_result)
        cmd = f"mv " \
              f"{os.path.join(self.parent_path_sub, 'pg_hba.conf')} " \
              f"{os.path.join(macro.DB_INSTANCE_PATH_REMOTE1, 'pg_hba.conf')} "
        self.log.info(cmd)
        sh_result1 = self.pri_userdb_sub.sh(cmd).result()
        self.log.info(sh_result1)
        result1 = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        self.log.info(result1)
        result2 = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"enable_incremental_checkpoint="
            f"'{self.enable_incremental_checkpoint}'")
        self.log.info(result2)
        result3 = self.commsh_sub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"enable_incremental_checkpoint="
            f"'{self.enable_incremental_checkpoint1}'",
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1,
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(result3)
        pub_result = self.commsh_pub.restart_db_cluster(True)
        sub_result = self.commsh_sub.restart_db_cluster(
            True, macro.DB_ENV_PATH_REMOTE1)
        self.assertTrue(pub_result, '执行失败' + text)
        self.assertTrue(sub_result, '执行失败' + text)
        self.assertTrue(result1, '执行失败' + text)
        self.assertTrue(result2, '执行失败' + text)
        self.assertTrue(result3, '执行失败' + text)
        self.assertTrue(result3, '执行失败' + text)
        self.assertEqual("", sh_result, '执行失败' + text)
        self.assertEqual("", sh_result1, '执行失败' + text)
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_result,
                      '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result_sub, '执行失败'
                      + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result_pub, '执行失败'
                      + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, result_sub,
                      '执行失败' + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, result_pub,
                      '执行失败' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
